<?php

namespace App\Admin\Controllers;

use App\Helpers\Api\ApiResponse;
use App\Http\Controllers\Controller;
use App\Models\Withdraw;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;

class WithdrawController extends Controller
{
    use ModelForm, ApiResponse;

    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('提现');
            $content->description('管理');

            $content->body($this->grid());
        });
    }

    protected function grid()
    {
        return Admin::grid(Withdraw::class, function (Grid $grid) {
            $grid->model()->orderBy("id", "desc");
            $grid->id('ID')->sortable();
            //$grid->order_sn("流水号");
            $grid->user_info('用户')->display(function () {
                return '用户：' . $this->user->name . "<br/>总收入：" . $this->user->sum_money . "<br/>当前余额：" . $this->user->money
                    . "<br/>提现总额：" . $this->user->withdraw()->whereIn('state',[0,1])->sum('money');
            });
            $grid->column('money', '提现金额')->display(function ($v) {
                return "<span class='label label-success'>$v</span>";
            });
            $grid->column('ac_type', '提现账户类型');
            $grid->column('ac_nickname', '提现账户名称');
            $grid->column('ac_username', '提现账户');
            $grid->created_at('提现时间');
            $grid->remark('备注');
            $grid->column('state', '状态')->display(function ($s) {
                switch ($s) {
                    case 0:
                        return "<span class='label label-default'>待审核</span>";
                        break;
                    case 1:
                        return "<span class='label label-success'>已打款</span>";
                        break;
                    case 2:
                        return "<span class='label label-danger'>审核不通过</span>";
                        break;
                    case 3:
                        return "<span class='label label-warning'>失败已退款</span>";
                        break;
                }
            });
            $grid->disableCreateButton();
            $grid->disableRowSelector();

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableEdit();
                $actions->disableDelete();
                $js = "layer.open({
  type: 2,
  title: false,
  shadeClose: true,
  shade: 0.8,
  area: ['480px', '180px'],
  content: '" . route('admin-withdraw-check', ['id' => $actions->getKey()]) . "'
}); ";
                if ($actions->row->state == 0) {
                    $actions->append('<a class="btn btn-xs btn-info" onclick="' . $js . '">审核</a>');
                } elseif ($actions->row->state == 2) {
                    $actions->append('<a class="btn btn-xs btn-danger" onclick="' . $js . '">退款</a>');
                }

            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->disableIdFilter();
                $filter->equal('state', '状态')->select(['0' => '未审核', '1' => '已打款', '2' => '不通过', '3' => '已退款']);
            });

            $grid->tools(function (Grid\Tools $tools) {
                $tools->disableRefreshButton();
                $tools->append('<div class="btn-group">');
                $tools->append('<a href="' . admin_url('withdraw') . '" class="btn btn-default btn-sm">全部</a>');
                $tools->append('<a href="' . admin_url('withdraw?state=0') . '" class="btn btn-default btn-sm">未审核</a>');
                $tools->append('<a href="' . admin_url('withdraw?state=1') . '" class="btn btn-default btn-sm">已打款</a>');
                $tools->append('<a href="' . admin_url('withdraw?state=2') . '" class="btn btn-default btn-sm">不通过</a>');
                $tools->append('<a href="' . admin_url('withdraw?state=3') . '" class="btn btn-default btn-sm">已退款</a>');
                $tools->append('</div>');
            });
        });

    }


    public function ckeck(Request $request)
    {
        $id = $request->input("id");
        $data['id'] = $id;
        $data['wd'] = Withdraw::query()->findOrFail($id);
        return view('admin.withdraw-check', $data);
    }

    public function check_data(Request $request)
    {
        $id = $request->input("id");
        $state = $request->input("state");
        $remark = $request->input("remark");

        list($state, $message) = Withdraw::ckeck($id, $state, $remark);
        if ($state) {
            return $this->message($message);
        } else {
            return $this->failed($message);
        }


    }

}
